#!/bin/sh
# Helper script to create a symlink to sharedlib,
#   like libgauche-0.9.so -> libgauche-0.9.so.0.1
# Assumes $TARGETLIB and $DESTDIR is set by the caller.

# In some occasions, TARGETLIB contains a path like
# '@executable_path/../Frameworks/Gauche.framework'.
# We don't need to create links in such case.

# NB: In 0.9.x series, we create a link libgauche.so -> libgauche-0.9.so
# for the backward comatibility.  This feature should be removed on 1.0
# release.

set -e

case "$TARGETLIB" in
  "@*")
    exit 0;;
  "")
    echo "TAGRETLIB must be set"
    exit 1;;
esac

mode=create
case "$1" in
  -d )
    mode=delete
    shift;;
  -* )
    mode=help
    shift;;
esac

if test \( $# != 1 \) -o $mode = "help"; then
  echo "Usage: makeverslink [-d] LIBGAUCHE"
  echo "LIBGAUCHE is the name of Gauche DSO, e.g. libgauche-0.9.so"
  echo "Environment variable TARGETLIB and DESTDIR must be set"
  echo "If -d option is given, remove links instead of creating"
  exit 0
fi


LIBGAUCHE=$1

MICRO_VERSION=@GAUCHE_MICRO_VERSION@

LIB_VVV=$LIBGAUCHE.0.$MICRO_VERSION
LIB_V=$LIBGAUCHE.0
LIB_COMPAT=libgauche.so

cd "$DESTDIR$TARGETLIB"
case "$mode" in
  create)
    # avoid re-running mkverslink.
    if test -f $LIB_VVV -a -L $LIBGAUCHE; then exit; fi
    rm -f $LIB_VVV
    mv $LIBGAUCHE $LIB_VVV
    ln -s $LIB_VVV $LIBGAUCHE
    rm -f $LIB_V
    ln -s $LIB_VVV $LIB_V

    if [ -f $LIB_COMPAT ]; then
      rm -f $LIB_COMPAT
      ln -s $LIBGAUCHE $LIB_COMPAT
    fi
    if [ -f $LIB_COMPAT.0 ]; then
      rm -f $LIB_COMPAT.0
      ln -s $LIB_V $LIB_COMPAT.0
    fi
    if [ -f $LIB_COMPAT.0.9.0 ]; then
      rm -f $LIB_COMPAT.0.9.0
      ln -s $LIB_VVV $LIB_COMPAT.0.9.0
    fi
    ;;
  delete)
    rm -f $LIB_VVV $LIB_V $LIBGAUCHE $LIB_COMPAT $LIB_COMPAT.0 $LIB_COMPAT.0.9.0
    ;;
esac

